package com.hw.servlet;

import com.hw.dao.CountriesDao;
import com.hw.entity.Countries;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "QServlet",urlPatterns = "/qq")
public class QServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("utf-8");
        CountriesDao countriesDao=new CountriesDao();
       List<Countries> countries=countriesDao.findAll();
        StringBuffer strings=new StringBuffer("<html>" +
                "\t<head>\n" +
                "\t\t<meta charset=\"utf-8\">\n" +
                "\t\t<title></title>\n" +
                "\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n\t\t" +
                "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.staticfile.org/twitter-bootstrap/3.4.0/css/bootstrap.min.css\" />" +
                "\n" +
                "\t</head>\n" +
                "<body>");
        response.setCharacterEncoding("UTF-8");
        PrintWriter printWriter=response.getWriter();
        strings.append("<table id=\"table\" class=\"table table-bordered\">\n" +
                "\t\t\t<thead>\n" +
                "\t\t\t\t<tr>\n" +
                "\t\t\t\t\t<th>国家简写</th>\n" +
                "\t\t\t\t\t<th>国家名称</th>\n" +
                "\t\t\t\t\t<th>国家代号</th>\n" +
                "\t\t\t\t</tr>\n" +
                "\t\t\t</thead>\n" +
                "\t\t\t<tbody id=\"tbody\">" );
       for (Countries c:countries){
           System.out.println(c.getCountryId());
         strings.append("<tr><td>"+c.getCountryId()+"</td><td>"+c.getCountryName()+"</td><td>"+c.getRegionId()+"</td></tr>");
       }

        strings.append("</tbody></table></body></html>");
        printWriter.print(strings.toString());


    }
}
